package com.totalinlink.util;

import android.util.Log;

import com.android.volley.Cache;
import com.android.volley.Cache.Entry;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * Created by zhangpf1127 on 2015/8/25.
 */
public class HttpUtil {


    private static final String TAG = HttpUtil.class.getSimpleName();

    private static JSONObject resJSONObject = new JSONObject();

    private static JSONArray resJSONArray = new JSONArray();

    /**
     * GET请求
     * @param url
     * @return
     */
    public static JSONObject getRequest(String url) {

        JsonObjectRequest req = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());
                resJSONObject = response;
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d(TAG, "Error: " + volleyError.getMessage());
            }
        });

        JSONObject result = resJSONObject;
        return result;
    }

    /**
     * POST请求 JSONObject
     * @param url
     * @param params
     * @return
     */
    public static JSONObject postJsonObjectRequest(String url, JSONObject params) {

        JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, url, params,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, response.toString());
                        resJSONObject = response;
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d(TAG, "Error: " + volleyError.getMessage());
            }
        });
        ApplicationController.getInstance().addToRequestQueue(req, TAG);

        JSONObject result = resJSONObject;
        return result;
    }

    /**
     *
     * @param url
     * @return
     */
    public static JSONArray postJsonArrayRequest(String url) {

        JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                Log.d(TAG, response.toString());
                resJSONArray = response;
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d(TAG, "Error: " + volleyError.getMessage());
            }

        });
        ApplicationController.getInstance().addToRequestQueue(req, TAG);

        return resJSONArray;
    }

    public static JSONObject cacheRequest(String url, JSONObject params) {

        JSONObject result = new JSONObject();

        Cache cache = ApplicationController.getInstance().getRequestQueue().getCache();

        Entry entry = cache.get(url);

        if(entry != null){
            try {
                String data = new String(entry.data, "UTF-8");
                try {
                    result = new JSONObject(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        } else {
            // request again
            result = postJsonObjectRequest(url, params);
        }
        return result;
    }
}
